package com.redis.lesson.redissettaobaoblack.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author gusheng
 * @Date 2021/1/7 19:45
 * @ClassName 类名称
 * @Description 类描述
 * @Version 3.4
 */
@RestController
@Api("黑名单模块接口")
public class BlackListController {

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String BLACK_USERID = "blackName";

    @RequestMapping("/filterBlack")
    @ApiOperation("添加黑名单userId  1-100")
    public void addBlack(){
        List<Integer> blackList = getBlackList();
        String key = BLACK_USERID;
        for (int i = 0; i < 100 ; i++) {
            this.redisTemplate.opsForSet().add(key,i);
        }
    }

    @RequestMapping("/getBlack")
    @ApiOperation("得是否是黑名单成员，true为黑名单")
    public Boolean getBlack(Integer userId){
        List<Integer> blackList = getBlackList();
        String key = BLACK_USERID;
        Boolean isBlack = redisTemplate.opsForSet().isMember(key, userId);
        //true 黑名单    false 不在黑名单
        return isBlack;
    }

    public List<Integer> getBlackList(){
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 100 ; i++) {
            list.add(i);
        }
        return list;
    }
}
